﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WeatherStation;

namespace WeatherStationView
{
    /// <summary>
    /// Interaction logic for CurrentConditionsDisplay.xaml
    /// </summary>
    public partial class CurrentConditionsDisplay : UserControl, IObserver
    {
        public CurrentConditionsDisplay()
        {
            InitializeComponent();
        }

        #region CurrentConditions variables

        private float temperature;
        private float humidity;
        private float pressure;

        #endregion

        #region IObserver Members

        public void Update(float temperature, float humidity, float pressure)
        {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new setValuesDelegate(setValues), temperature, humidity);
        }

        #endregion

        #region WeatherData suscription related

        private Boolean suscribedToWD = false;

        private ISubject wd;

        public ISubject WeatherDataSource
        {
            get
            {
                return wd;
            }
            set
            {
                wd = value;
            }
        }

        #endregion

        #region GUI logic

        private delegate void setValuesDelegate(float temperature, float humidity);

        private void setValues(float temperature, float humidity)
        {
            pgbTemperature.Value = temperature;
            lblTemperature.Content = temperature.ToString("#0.00") + "º";

            pgbHumidity.Value = humidity;
            lblHumidity.Content = humidity.ToString("#0.00") + "%";
        }

        private void btnSuscribeUnsuscribe_Click(object sender, RoutedEventArgs e)
        {
            if (wd == null) return;

            if (suscribedToWD)
            {
                wd.RemoveObserver(this);
                btnSuscribeUnsuscribe.Content = "Suscribe to Weather Data Source";
            }
            else
            {
                wd.RegisterObserver(this);
                btnSuscribeUnsuscribe.Content = "Unsuscribe from Weather Data Source";
            }

            suscribedToWD = !suscribedToWD;
        }

        #endregion
    }
}
